#	Copyright (C) 2008 Sambit Bikas Pal, IISERK
#	Author: Sambit Bikas Pal , Email: sam@botcyb.org , sambit@iiserkol.ac.in
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
__author__ = 'Sambit Bikas Pal'

DEBUG = True

from google.appengine.api import images
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from editable import heads		#Contains stuffs to be inserted in heads of various pages
from editable import feet		#Contains stuffs to be put at the end of various pages
from editable import forms
import dbase

class UserAddStep0(webapp.RequestHandler):
	def collect(self):
		head = heads.heads()				#Fetches the heads
		self.head = head.USER_ADD_FORM
		foot = feet.feet()				#Fetches the feet
		self.foot = foot.USER_ADD_FORM
		form = forms.forms()
		self.form = form.UAS0

	def create_html(self):
		self.html="""
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			""" + self.head + """
			<body>
			""" + self.form + """
			""" + self.foot + """
			</body>
			</html>
			"""

	def get(self):
		self.collect()
		self.create_html()
		self.response.headers['Content-Type'] = 'text/html'
		self.response.out.write(self.html)			#Spews out the page
		
		
class UserAddStep1(webapp.RequestHandler):
	def collect(self):
		head = heads.heads()				#Fetches the heads
		self.head = head.USER_ADD_FORM
		foot = feet.feet()				#Fetches the feet
		self.foot = foot.USER_ADD_FORM
		form = forms.forms()
		self.form1 = form.UAS1_Option1
		self.form2 = form.UAS1_Option2

	def create_html1(self):
		self.html="""
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			""" + self.head + """
			<body>
			""" + self.form1 + """
			""" + self.foot + """
			</body>
			</html>
			"""
	def create_html2(self):
		self.html="""
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			""" + self.head + """
			<body>
			""" + self.form2 + """
			""" + self.foot + """
			</body>
			</html>
			"""

	def post(self):
		acstatus = self.request.get('account')
		self.collect()
		if acstatus == "I have an iiserk.net account":
			self.create_html1()
			self.response.headers['Content-Type'] = 'text/html'
			self.response.out.write(self.html)			#Spews out the page
		if acstatus == "I do not have one":
			self.create_html2()
			self.response.headers['Content-Type'] = 'text/html'
			self.response.out.write(self.html)			#Spews out the page
		

class UserAddStep2(webapp.RequestHandler):
	def collect(self):
		head = heads.heads()				#Fetches the heads
		self.head = head.USER_ADD_FORM
		foot = feet.feet()				#Fetches the feet
		self.foot = foot.USER_ADD_FORM
		form = forms.forms()
		self.noexist = form.UAS2_Option1
		self.exist = form.UAS2_Option2
		self.greeting = form.UAS2_Greeting



	def create_html1(self):
		self.html="""
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			""" + self.head + """
			<body>
			""" + self.noexist + """
			</body>
			</html>
		"""

	def create_html2(self):
		self.html='''
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			''' + self.head + '''
			<body>
			''' + self.exist + '''
			</body>
			</html>
		'''



	
	def get(self):
		loggedin = users.get_current_user()
		self.collect()
		allusers = db.GqlQuery("SELECT * FROM User ORDER BY username")
		flag=0
		for user in allusers:
			if loggedin.nickname() == user.username:
				flag=1
				if user.userfullname != None:
					fullname = user.userfullname
				else:
					fullname =""
				if user.useremail != None:
					email = user.useremail
				else:
					email =""
				if user.usermajor != None:
					major = user.usermajor
				else:
					major =""
				if user.useryear != None:
					year = user.useryear
				else:
					year =""
				if user.userbatch != None:
					batch = user.userbatch
				else:
					batch =""
				if user.userabout != None:
					about = user.userabout
				else:
					about =""
				if user.userblog != None:
					blog = user.userblog
				else:
					blog =""
				if user.userweb != None:
					web = user.userweb
				else:
					web =""
				if user.userorkut != None:
					orkut = user.userorkut
				else:
					orkut =""
				if user.userphone != None:
					phone = user.userphone
				else:
					phone =""
				break
		if flag==0:
			self.create_html1()
			greeting = (self.greeting %(loggedin.nickname(), loggedin.nickname(), users.create_logout_url("/add/uas2")))
			self.response.headers['Content-Type'] = 'text/html'
			self.response.out.write((self.html)%(greeting,"/add/uas3"))
		else:
			self.create_html2()
			greeting = (self.greeting %(loggedin.nickname(), loggedin.nickname(), users.create_logout_url("/add/uas2")))
			### Generate the dropdown list
			form = forms.forms()
			majordrop = (form.UAS2_Majordrop %("","","",""))			
			if major == "Biology" :
				majordrop = (form.UAS2_Majordrop %("selected","","",""))
			if major == "Chemistry" :
				majordrop = (form.UAS2_Majordrop %("","selected","",""))
			if major == "Mathematics" :
				majordrop = (form.UAS2_Majordrop %("","","selected",""))
			if major == "Physics" :
				majordrop = (form.UAS2_Majordrop %("","","","selected"))
				
			yeardrop = (form.UAS2_Yeardrop %("","","","",""))			
			if year == "1st" :
				yeardrop = (form.UAS2_Yeardrop %("selected","","","",""))
			if year == "2nd" :
				yeardrop = (form.UAS2_Yeardrop %("selected","","","",""))
			if year == "3rd" :
				yeardrop = (form.UAS2_Yeardrop %("selected","","","",""))
			if year == "4th" :
				yeardrop = (form.UAS2_Yeardrop %("selected","","","",""))
			if year == "5th" :
				yeardrop = (form.UAS2_Yeardrop %("selected","","","",""))
			
			batchdrop = (form.UAS2_Batchdrop %("","","","",""))
			if year == "2006-2011" :
				batchdrop = (form.UAS2_Batchdrop %("selected","","","",""))
			if year == "2007-2012" :
				batchdrop = (form.UAS2_Batchdrop %("selected","","","",""))
			if year == "2008-2013" :
				batchdrop = (form.UAS2_Batchdrop %("selected","","","",""))
			if year == "2009-2014" :
				batchdrop = (form.UAS2_Batchdrop %("selected","","","",""))
			if year == "2010-2015" :
				batchdrop = (form.UAS2_Batchdrop %("selected","","","",""))
			
			
			self.response.headers['Content-Type'] = 'text/html'
			self.response.out.write(self.html %(greeting, fullname, email, majordrop, yeardrop, batchdrop, about, blog, web, orkut, phone))


class UserAddStep3(webapp.RequestHandler):
	def collect(self):
		head = heads.heads()				#Fetches the heads
		self.head = head.USER_ADD_FORM
		foot = feet.feet()				#Fetches the feet
		self.foot = foot.USER_ADD_FORM
		form = forms.forms()
		self.form = form.UAS3
	
	def create_html(self):
		pass
	def post(self):
		self.response.headers['Content-Type'] = 'text/html'
		self.response.out.write("Testing")


user = webapp.WSGIApplication([('/add/uas0' ,UserAddStep0),
				('/add/uas1' ,UserAddStep1),
				('/add/uas2' ,UserAddStep2),
				('/add/uas3' ,UserAddStep3)
#				('/add/uas4' ,UserAddStep4),
				],debug=DEBUG)


def main():
	run_wsgi_app(user)

if __name__ == "__main__":
	main()
